{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "91cfc574",
   "metadata": {},
   "source": [
    "# DataFrame\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b99ecea2",
   "metadata": {},
   "source": [
    "## Quick Start"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "812322f9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AAA  BBB  CCC\n",
       "A    1    5    9\n",
       "B    2    6   10\n",
       "C    3    7   11\n",
       "D    4    8   12"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "test_df = pd.DataFrame(data=\n",
    "            {\n",
    "                \"AAA\":[1,2,3,4],\n",
    "                \"BBB\":[5,6,7,8],\n",
    "                \"CCC\":[9,10,11,12]\n",
    "            },index=[\"A\",\"B\",\"C\",\"D\"])\n",
    "test_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0926798",
   "metadata": {},
   "source": [
    "### if-else\n",
    "key word: loc"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "256de257",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>3</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AAA  BBB  CCC\n",
       "A    1    5    9\n",
       "B    2   10   10\n",
       "C    3   10   10\n",
       "D    4   10   10"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df.loc[test_df.AAA>=2,[\"BBB\",\"CCC\"]] = 10\n",
    "test_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52809f15",
   "metadata": {},
   "source": [
    "### if-then-else\n",
    "key word: where\n",
    "1. pandas DataFrame where\n",
    "2. numpy where"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a7dbf2bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "      <th>logic</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>9</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>10</td>\n",
       "      <td>low</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>11</td>\n",
       "      <td>high</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "      <td>high</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AAA  BBB  CCC logic\n",
       "A    1    5    9   low\n",
       "B    2    6   10   low\n",
       "C    3    7   11  high\n",
       "D    4    8   12  high"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "test_df = pd.DataFrame(data=\n",
    "            {\n",
    "                \"AAA\":[1,2,3,4],\n",
    "                \"BBB\":[5,6,7,8],\n",
    "                \"CCC\":[9,10,11,12]\n",
    "            },index=[\"A\",\"B\",\"C\",\"D\"])\n",
    "\n",
    "test_df[\"logic\"] = np.where(test_df[\"AAA\"]>2,\"high\",\"low\")\n",
    "test_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef031b42",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_df[\"new_logic\"] = test_df.where()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "33213c4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2.0,\n",
       " 2.4210526315789473,\n",
       " 2.8421052631578947,\n",
       " 3.263157894736842,\n",
       " 3.6842105263157894,\n",
       " 4.105263157894736,\n",
       " 4.526315789473684,\n",
       " 4.947368421052632,\n",
       " 15.0,\n",
       " 15.5]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numpy where\n",
    "# np.where(condition, x, y)\n",
    "aa = 5 * np.ones(10)\n",
    "xx = np.linspace(2,10,20)\n",
    "yy = np.arange(11,25,0.5)\n",
    "\n",
    "l = [x if x<a else y for a,x,y in zip(aa,xx,yy)]\n",
    "l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "db0b7fc4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>10</td>\n",
       "      <td>10</td>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>4</td>\n",
       "      <td>8</td>\n",
       "      <td>10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AAA  BBB  CCC\n",
       "A    1   10   10\n",
       "B   10   10   10\n",
       "C   10   10   11\n",
       "D    4    8   10"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pandas DataFrame虽然是二维numpy array \n",
    "# 但是其数据定位仍旧是行+列的双重格式\n",
    "test_df = pd.DataFrame(data=\n",
    "            {\n",
    "                \"AAA\":[1,2,3,4],\n",
    "                \"BBB\":[5,6,7,8],\n",
    "                \"CCC\":[9,10,11,12]\n",
    "            },index=[\"A\",\"B\",\"C\",\"D\"])\n",
    "\n",
    "c1 = np.array([1,0,0,1],dtype=np.bool_)\n",
    "c2 = np.array([0,0,0,1],dtype=np.bool_)\n",
    "c3 = np.array([0,0,1,0],dtype=np.bool_)\n",
    "where_mask = np.stack([c1,c2,c3],axis=1)\n",
    "where_masks = pd.DataFrame(where_mask,columns=[\"AAA\",\"BBB\",\"CCC\"],index=[\"A\",\"B\",\"C\",\"D\"])\n",
    "\n",
    "test_df.where(where_masks,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7f8e78ce",
   "metadata": {},
   "source": [
    "### df.loc iloc []\n",
    "1. df.loc[] -- label oriented 是闭区间\n",
    "2. df.iloc[] -- position oriented 是右开区间\n",
    "3. df[] 是选择，第一个参数是条件，第二个参数是列范围\n",
    "4. df.index.isin() -- 一个很好用的方法，通过inedx进行选择"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "ec8a8f9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.395320</td>\n",
       "      <td>-0.361420</td>\n",
       "      <td>0.563441</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>B</th>\n",
       "      <td>-1.306173</td>\n",
       "      <td>1.958097</td>\n",
       "      <td>-0.851067</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>C</th>\n",
       "      <td>-0.644516</td>\n",
       "      <td>0.638778</td>\n",
       "      <td>0.098307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>D</th>\n",
       "      <td>1.622761</td>\n",
       "      <td>-0.804671</td>\n",
       "      <td>-0.034382</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        AAA       BBB       CCC\n",
       "A  0.395320 -0.361420  0.563441\n",
       "B -1.306173  1.958097 -0.851067\n",
       "C -0.644516  0.638778  0.098307\n",
       "D  1.622761 -0.804671 -0.034382"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_array = np.random.normal(0,1,(4,3))\n",
    "test_df = pd.DataFrame(data=test_array,columns=[\"AAA\",\"BBB\",\"CCC\"],index=[\"A\",\"B\",\"C\",\"D\"])\n",
    "\n",
    "test_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "a3159852",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>A</th>\n",
       "      <td>0.39532</td>\n",
       "      <td>-0.36142</td>\n",
       "      <td>0.563441</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       AAA      BBB       CCC\n",
       "A  0.39532 -0.36142  0.563441"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_df[(test_df.AAA>0.3)&(test_df.index.isin([\"A\",\"C\"]))]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f607c397",
   "metadata": {},
   "source": [
    "### dynamic column creation\n",
    "1. python built in function: **apply applymap**\n",
    "2. pandas column inner logic: just as list - **DataFrame.columns**\n",
    "3. pandas **lambda function** - functional programming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "303ed250",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AAA  BBB  CCC\n",
       "0    1    1    2\n",
       "1    2    3    6\n",
       "2    4    5    3\n",
       "3    6    2    4"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_df = pd.DataFrame(data={\n",
    "    \"AAA\": [1,2,4,6],\n",
    "    \"BBB\": [1,3,5,2],\n",
    "    \"CCC\": [2,6,3,4]\n",
    "})\n",
    "\n",
    "col_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "6b846d81",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>AAA</th>\n",
       "      <th>BBB</th>\n",
       "      <th>CCC</th>\n",
       "      <th>AAA_pet</th>\n",
       "      <th>BBB_pet</th>\n",
       "      <th>CCC_pet</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>Cat</td>\n",
       "      <td>Cat</td>\n",
       "      <td>Dog</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>6</td>\n",
       "      <td>Dog</td>\n",
       "      <td>Lizard</td>\n",
       "      <td>Goldfish</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>Snake</td>\n",
       "      <td>Rabbit</td>\n",
       "      <td>Lizard</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>Goldfish</td>\n",
       "      <td>Dog</td>\n",
       "      <td>Snake</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   AAA  BBB  CCC   AAA_pet BBB_pet   CCC_pet\n",
       "0    1    1    2       Cat     Cat       Dog\n",
       "1    2    3    6       Dog  Lizard  Goldfish\n",
       "2    4    5    3     Snake  Rabbit    Lizard\n",
       "3    6    2    4  Goldfish     Dog     Snake"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categories = {\n",
    "    1: \"Cat\",\n",
    "    2: \"Dog\",\n",
    "    3: \"Lizard\",\n",
    "    4: \"Snake\",\n",
    "    5: \"Rabbit\",\n",
    "    6: \"Goldfish\"\n",
    "}\n",
    "\n",
    "source_col = col_df.columns\n",
    "new_col = [str(x)+\"_pet\" sfor x in source_col]\n",
    "col_df[new_col] = col_df[source_col].applymap(categories.get)\n",
    "\n",
    "col_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5dcdf7c6",
   "metadata": {},
   "source": [
    "### apply applymap [only used for pandas, depricated in python]\n",
    "1. apply is mainly used for Series - which is only 1D array\n",
    "2. applymap is mainly used for DataFrame - which is 2D array\n",
    "3. new function **numpy.random.shuffle**: return None, just shuffle data from original array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "deb0c150",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>22.340426</td>\n",
       "      <td>11.702128</td>\n",
       "      <td>14.255319</td>\n",
       "      <td>24.042553</td>\n",
       "      <td>16.808511</td>\n",
       "      <td>20.638298</td>\n",
       "      <td>20.212766</td>\n",
       "      <td>18.936170</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>15.531915</td>\n",
       "      <td>12.127660</td>\n",
       "      <td>24.893617</td>\n",
       "      <td>18.510638</td>\n",
       "      <td>17.659574</td>\n",
       "      <td>27.021277</td>\n",
       "      <td>22.765957</td>\n",
       "      <td>10.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10.851064</td>\n",
       "      <td>19.361702</td>\n",
       "      <td>29.148936</td>\n",
       "      <td>15.106383</td>\n",
       "      <td>25.319149</td>\n",
       "      <td>13.404255</td>\n",
       "      <td>18.085106</td>\n",
       "      <td>23.191489</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>27.446809</td>\n",
       "      <td>17.234043</td>\n",
       "      <td>10.425532</td>\n",
       "      <td>26.170213</td>\n",
       "      <td>28.723404</td>\n",
       "      <td>21.914894</td>\n",
       "      <td>12.553191</td>\n",
       "      <td>14.680851</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>23.617021</td>\n",
       "      <td>21.063830</td>\n",
       "      <td>30.000000</td>\n",
       "      <td>24.468085</td>\n",
       "      <td>28.297872</td>\n",
       "      <td>21.489362</td>\n",
       "      <td>26.595745</td>\n",
       "      <td>19.787234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>13.829787</td>\n",
       "      <td>12.978723</td>\n",
       "      <td>11.276596</td>\n",
       "      <td>15.957447</td>\n",
       "      <td>16.382979</td>\n",
       "      <td>27.872340</td>\n",
       "      <td>25.744681</td>\n",
       "      <td>29.574468</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           0          1          2          3          4          5  \\\n",
       "0  22.340426  11.702128  14.255319  24.042553  16.808511  20.638298   \n",
       "1  15.531915  12.127660  24.893617  18.510638  17.659574  27.021277   \n",
       "2  10.851064  19.361702  29.148936  15.106383  25.319149  13.404255   \n",
       "3  27.446809  17.234043  10.425532  26.170213  28.723404  21.914894   \n",
       "4  23.617021  21.063830  30.000000  24.468085  28.297872  21.489362   \n",
       "5  13.829787  12.978723  11.276596  15.957447  16.382979  27.872340   \n",
       "\n",
       "           6          7  \n",
       "0  20.212766  18.936170  \n",
       "1  22.765957  10.000000  \n",
       "2  18.085106  23.191489  \n",
       "3  12.553191  14.680851  \n",
       "4  26.595745  19.787234  \n",
       "5  25.744681  29.574468  "
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apply_array = np.linspace(10,30,48)\n",
    "np.random.shuffle(apply_array)\n",
    "apply_array = apply_array.reshape(6,8)\n",
    "\n",
    "apply_df = pd.DataFrame(data=apply_array)\n",
    "apply_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "cd2d1b4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    12.340426\n",
       "1    17.021277\n",
       "2    18.297872\n",
       "3    18.297872\n",
       "4    10.212766\n",
       "5    18.297872\n",
       "dtype: float64"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# when axis=1, all operations are implemented over rows\n",
    "apply_df.apply(lambda x: max(x) - min(x),axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "6848a01f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    16.595745\n",
       "1     9.361702\n",
       "2    19.574468\n",
       "3    11.063830\n",
       "4    12.340426\n",
       "5    14.468085\n",
       "6    14.042553\n",
       "7    19.574468\n",
       "dtype: float64"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# when axis=0, all operations are implemented over columns\n",
    "apply_df.apply(lambda x: max(x) - min(x), axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "d3390e8c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   0  1  2  3  4  5  6  7\n",
       "0  1  0  0  1  0  1  1  0\n",
       "1  0  0  1  0  0  1  1  0\n",
       "2  0  0  1  0  1  0  0  1\n",
       "3  1  0  0  1  1  1  0  0\n",
       "4  1  1  1  1  1  1  1  0\n",
       "5  0  0  0  0  0  1  1  1"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "apply_df.applymap(lambda x: 1 if x>20 else 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18900773",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
